<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 菜单管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.menuId" placeholder="菜单编号" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-input v-model="query.menuTitle" placeholder="菜单名称" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="el-button" icon="el-icon-delete" @click="handleReset">重置</el-button>
                <div class="right-items" style="float: right;">
                    <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
                </div>
            </div>
            <el-table
                    :data="tableData"
                    style="width: 100%"
                    row-key="menuId"
                    class="table"
                    border
                    lazy
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="menuId" label="菜单编号" width="200" align="center"></el-table-column>
                <el-table-column prop="menuTitle" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="menuIcon" label="菜单图标" align="center"></el-table-column>
                <el-table-column prop="menuIndex" label="菜单URL" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%" :close-on-click-modal="false">
            <el-form ref="editForm" :rules="editFormRules" :model="editForm" label-width="100px">
                <el-form-item label="菜单编号：" prop="menuId">
                    <el-input readonly v-model="editForm.menuId"></el-input>
                </el-form-item>
                <el-form-item label="菜单名称：" prop="menuTitle">
                    <el-input v-model="editForm.menuTitle"></el-input>
                </el-form-item>
                <el-form-item label="菜单URL：" prop="menuIndex">
                    <el-input v-model="editForm.menuIndex"></el-input>
                </el-form-item>
                <el-form-item label="菜单图标：" prop="menuIcon">
                    <el-input v-model="editForm.menuIcon"></el-input>
                </el-form-item>
                <el-form-item label="菜单序号：" prop="menuOdr">
                    <el-input v-model="editForm.menuOdr" onkeyup="value = value.replace(/[^\d]/g,'')" placeholder="请输入数字"></el-input>
                </el-form-item>
                <el-form-item label="父菜单编号：" prop="parentMenuId">
                    <el-input v-model="editForm.parentMenuId"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeEdit">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%" :close-on-click-modal="false">
            <el-form ref="addForm" :rules="addFormRules" :model="addForm" label-width="100px">
                <el-form-item label="菜单编号：" prop="menuId">
                    <el-input v-model="addForm.menuId" onkeyup="value = value.replace(/[^\d]/g,'')" placeholder="请输入数字"></el-input>
                </el-form-item>
                <el-form-item label="菜单名称：" prop="menuTitle">
                    <el-input v-model="addForm.menuTitle"></el-input>
                </el-form-item>
                <el-form-item label="菜单URL：" prop="menuIndex">
                    <el-input v-model="addForm.menuIndex"></el-input>
                </el-form-item>
                <el-form-item label="菜单图标：" prop="menuIcon">
                    <el-input v-model="addForm.menuIcon"></el-input>
                </el-form-item>
                <el-form-item label="菜单序号：" prop="menuOdr">
                    <el-input v-model="addForm.menuOdr" onkeyup="value = value.replace(/[^\d]/g,'')" placeholder="请输入数字"></el-input>
                </el-form-item>
                <el-form-item label="父菜单编号：" prop="parentMenuId">
                    <el-input v-model="addForm.parentMenuId"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { menuData, doAdd, doDelete, isNotexistsUppMenu, doUpdate } from '../../../../api/system/menu/menu';
    export default {
        name: 'menus',
        data() {
            return {
                query: {
                    menuId: '',
                    menuTitle: '',
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                addVisible: false,
                pageTotal: 0,
                editForm: {},
                addForm: {},
                addFormRules: {
                    menuId: [
                        { required: true, message: '请输入菜单编号', trigger: 'blur' }
                    ],
                    menuTitle: [
                        { required: true, message: '请输入菜单名称', trigger: 'blur' }
                    ],
                    menuIndex: [
                        { required: true, message: '请输入菜单url', trigger: 'blur' }
                    ],
                    menuOdr: [
                        { required: true, message: '请输入序号', trigger: 'blur' }
                    ]
                },
                editFormRules: {
                    menuId: [
                        { required: true, message: '请输入菜单编号', trigger: 'blur' }
                    ],
                    menuTitle: [
                        { required: true, message: '请输入菜单名称', trigger: 'blur' }
                    ],
                    menuIndex: [
                        { required: true, message: '请输入菜单url', trigger: 'blur' }
                    ],
                    menuOdr: [
                        { required: true, message: '请输入序号', trigger: 'blur' }
                    ]
                },
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取后台数据
            getData() {
                menuData(this.query).then(res => {
                    if (res.success == true){
                        this.tableData = res.data;
                        this.pageTotal = res.total;
                    } else {
                        this.$message.error("查询失败！");
                        this.tableData = [];
                        this.pageTotal = 0;
                    }
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 触发重置按钮
            handleReset() {
                this.query.menuId = "";
                this.query.menuTitle = "";
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    //后台进行删除操作
                    let condition={
                        menuId:row.menuId
                    };
                    doDelete(condition).then(res => {
                        if (res.success && res.data == 1){
                            this.$message.success('删除成功');
                            this.getData();
                        }else {
                            this.$message.error('删除失败');
                            this.getData();
                        }
                    });
                    }).catch(() => {
                    this.$message({
                        message:'已取消删除'
                    });
                });
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 编辑操作
            handleEdit(index, row) {
                this.editForm = {};
                this.idx = index;
                this.editForm = row;
                this.editVisible = true;
            },
            closeEdit(){//关闭编辑弹出框
                this.editVisible=false;
                this.editForm={};
                this.getData();
            },
            // 保存编辑
            saveEdit() {
                this.$refs['editForm'].validate((valid) => {
                    if (valid) {
                        /*先判断菜单id是否存在*/
                        let isNotexists = {
                            menuId:this.editForm.menuId
                        };
                        isNotexistsUppMenu(isNotexists).then(res => {
                            if (res.success && !res.data){
                                if (this.editForm.parentMenuId != null && this.editForm.parentMenuId != ""){
                                    /*判断父菜单id是否存在*/
                                    let isExistsByParent = {
                                        menuId:this.editForm.parentMenuId
                                    };
                                    isNotexistsUppMenu(isExistsByParent).then(res => {
                                        if (res.success && !res.data){
                                            this.doUpdate();
                                        } else {
                                            this.$message.error('父菜单id不存在');
                                        }
                                    });
                                } else {
                                    this.doUpdate();
                                }
                            } else {
                                this.$message.error('父菜单id不存在');
                            }
                        });
                    }
                })
            },
            doUpdate(){
                if (this.editForm.menuId == this.editForm.parentMenuId){
                    this.$message.error('父菜单编号与菜单ID相同');
                    return;
                }
                this.editForm.children = null;
                doUpdate(this.editForm).then(res => {
                    if (res.success && res.data == 1){
                        this.editVisible = false;
                        this.editForm={};
                        this.getData();
                        this.$message.success('保存成功');
                    } else {
                        this.$message.error('保存失败');
                    }
                });
            },
            handleAdd() {
                this.addForm = {};
                this.addVisible = true;
            },
            saveAdd() {
                this.$refs['addForm'].validate((valid) => {
                    if (valid) {
                        /*先判断菜单id是否存在*/
                        let isNotexists = {
                            menuId:this.addForm.menuId
                        };
                        isNotexistsUppMenu(isNotexists).then(res => {
                            if (res.success && res.data){
                                if (this.addForm.parentMenuId != null && this.addForm.parentMenuId != ""){
                                    /*判断父菜单id是否存在*/
                                    let isExistsByParent = {
                                        menuId:this.addForm.parentMenuId
                                    };
                                    isNotexistsUppMenu(isExistsByParent).then(res => {
                                        if (res.success && !res.data){
                                            this.doAdd();
                                        } else {
                                            this.$message.error('父菜单id不存在');
                                        }
                                    });
                                } else {
                                    this.doAdd();
                                }
                            } else {
                                this.$message.error('菜单id已存在');
                            }
                        });
                    }
                });
            },
            doAdd(){
                doAdd(this.addForm).then(res => {
                    if (res.success && res.data == 1){
                        this.addVisible=false;
                        /*this.addForm={};*/
                        this.getData();
                        this.$message.success('保存成功');
                    } else {
                        this.$message.error(res.data);
                    }
                });
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            /*表单重置*/
            resetForm(refname){
                if (this.$refs[refname] !== undefined){
                    this.$refs[refname].resetFields();
                }
            },
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
</style>
